<template>
  <div>
    <el-form :model="form">
      <el-form-item label="广告标题">
        <el-input v-model="form.adverName" size="small"></el-input>
      </el-form-item>
<!--      <el-form-item label="广告图片">-->
<!--        <el-upload-->
<!--          class="upload"-->
<!--          :action = imageUploadURL-->
<!--          :show-file-list="false"-->
<!--          :headers="token"-->
<!--          :on-success="uploadSuccess">-->
<!--          <img v-if="form.appIcon" :src="form.appIcon" class="avatar" style="height:96px; width:96px" >-->
<!--          <i v-else class="el-icon-upload icon"></i>-->
<!--        </el-upload>-->
<!--      </el-form-item>-->
      <el-form-item label="状态：">
        <el-radio-group v-model="form.adverType">
          <el-radio label="1">链接</el-radio>
          <el-radio label="2">文本</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="添加链接" v-if="this.form.adverType==1">
        <el-input v-model="form.link" size="small"></el-input>
      </el-form-item>
      <el-form-item >
        <RichText :content="form.content" v-model="form.content" v-if="this.form.adverType==2" />
      </el-form-item>
      <el-form-item v-if="!form.adverId">
        <el-button size="small">取消</el-button>
        <el-button type="primary" size="small" @click="insertAdvertisement">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  import RichText from "@/components/RichText"
  import apis from "../../../apis/modules/advertisement"
  export default {
    components: {
      RichText
    },
    data() {
      return {
        form: {
          adverId : null,
          adverName:'',
          adverCover:'',
          adverType:'1',
          content:'',
          link : ''
        }
      }
    },
    created() {
      let adverId = this.$route.query.adverId
      if(adverId){
          this.form.adverId = adverId
          this.showAdvertisement()
      }
    },
    methods: {
      //增加广告
      insertAdvertisement(){
        apis.insertAdvertisement(this.form).then((res) => {
            console.log(res);
        });
      },
      showAdvertisement(){
        apis.showAdvertisement({adverId : this.adverId}).then((res) => {
          if(res.code == 200){
            this.form.adverName = res.data.adverName;
            this.form.adverCover = res.data.adverCover;
            this.form.adverType = res.data.adverType;
            if(res.data.adverType == 1){
              this.form.link = res.data.content
            }
            if(res.data.adverType == 2){
              this.form.content = res.data.content
            }
          }
        });
      }
    }
};
</script>
<style lang="scss" scoped>
  .el-input {
    width: 400px;
  }
</style>
